package com.tengniu.web.controller.buss;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tengniu.buss.domain.MassageChair;
import com.tengniu.buss.domain.Member;
import com.tengniu.buss.service.MemberService;
import com.tengniu.common.annotation.Log;
import com.tengniu.common.buss.enums.YesOrNoEnum;
import com.tengniu.common.core.controller.BaseController;
import com.tengniu.common.core.domain.AjaxResult;
import com.tengniu.common.core.page.TableDataInfo;
import com.tengniu.common.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 会员管理
 */
@RestController
@RequestMapping("/admin/member")
@Slf4j
public class MemberAdminController extends BaseController {

    @Resource
    private MemberService memberService;

    /**
     * 获取会员列表
     */
    @PreAuthorize("@ss.hasPermi('buss:member:list')")
    @PostMapping("/list")
    public TableDataInfo<Member> list(@RequestBody Member member) {
        startPage();
        List<Member> list = memberService.list(
                Wrappers.lambdaQuery(Member.class)
                        .eq(Objects.nonNull(member.getStatus()), Member::getStatus, member.getStatus())
                        .like(Objects.nonNull(member.getMemberName()), Member::getMemberName, member.getMemberName())
                        .like(Objects.nonNull(member.getMemberPhone()), Member::getMemberPhone, member.getMemberPhone()));
        return getDataTable(list);
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('buss:member:shelf')")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @GetMapping("/shelf/{memberId}")
    public AjaxResult shelf(@PathVariable Long memberId) {
        Member member = memberService.getById(memberId);
        if (member == null) {
            return AjaxResult.error("会员不存在");
        }
        member.setStatus(YesOrNoEnum.YES.getCode());
        return toAjax(memberService.updateById(member));
    }

    /**
     * 停用
     */
    @PreAuthorize("@ss.hasPermi('buss:member:memberId')")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @GetMapping("/offShelf/{memberId}")
    public AjaxResult offShelf(@PathVariable Long memberId) {
        Member member = memberService.getById(memberId);
        if (member == null) {
            return AjaxResult.error("会员不存在");
        }
        member.setStatus(YesOrNoEnum.NO.getCode());
        return toAjax(memberService.updateById(member));
    }

}
